package com.management.controller;

import com.management.po.*;
import com.management.service.CompanyService;
import com.management.service.FormStatsService;
import com.management.service.MaterialsService;
import com.management.service.PurchaseFormService;
import com.management.vo.MaterialsVO;
import com.management.vo.PurchaseFormVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author XS
 * 采购表业务逻辑类
 * @version 1.0
 */
@Controller
@RequestMapping("/purchaseform")
public class PurchaseFormController {
    Logger logger = Logger.getLogger(PurchaseFormController.class);
    @Autowired
    PurchaseFormService purchaseFormService;
    @Autowired
    FormStatsService formStatsService;
    @Autowired
    CompanyService companyService;
    @Autowired
    MaterialsService materialsService;
    //查询采购信息
    @RequestMapping("topurchaseformlist")
    public String topurchaseformlist(Model model){
        logger.info("进入查询采购信息控住器");
        List<PurchaseFormVO> list = purchaseFormService.findAll();
        System.out.println(list);
        model.addAttribute("list",list);
        return "purchaselist";
    }
    //查询该公司下的所有采购订单
    @RequestMapping("purchaseformlist")
    public String purchaseformlist(Model model,int companyId){
        logger.info("进入查询采购信息控住器");

        //根据公司编号查询采购订单信息
        List<PurchaseFormVO> list = purchaseFormService.findByCompanyId(companyId);
        System.out.println(list);
        model.addAttribute("list",list);

        return "purchaselist";
    }
    //进入修改采购信息控制器
    @RequestMapping("/toupdatepurchaseform")
    public String toUpdatePurchaseForm(String purchaseFormId,Model model,int companyId){
        logger.info("进入去修改采购信息控制器");
        //根据公司编号查询公司信息
        Company company=companyService.findByCompanyId(companyId);
        System.out.println(company.getCompanyName());
        List<FormStats> listStats=formStatsService.findAll();
        //根据公司编号查询物品库存信息
        List<MaterialsVO> listMater=materialsService.findByCompanyId(companyId);

        //根据采购订单编号查询订单信息
        PurchaseFormVO purchaseFormVO = purchaseFormService.findBy(purchaseFormId);
        model.addAttribute("purchaseFormVO",purchaseFormVO);
        model.addAttribute("listStats",listStats);
        model.addAttribute("listMater",listMater);
        return "updatepurchase";
    }
    //修改采购信息控制器
    @RequestMapping("/updatepurchase")
    public String UpdatePurchaseForm(PurchaseForm purchaseForm,int companyId){
        logger.info("进入修改采购信息控制器");
        System.out.println(purchaseForm);
        System.out.println(purchaseForm.getPurchaseNumber());
        boolean flag = purchaseFormService.update(purchaseForm);
        logger.debug(flag);
        return "redirect:http://localhost:8080/management/purchaseform/purchaseformlist.action?companyId="+companyId;
    }
    //取消采购信息控制器
    @RequestMapping("/delectpurchase")
    public String Delectpurchase(String purchaseFormId){
        logger.info("进入取消采购信息");

        System.out.println(purchaseFormId);
        boolean flag = purchaseFormService.delete(purchaseFormId);
        logger.info(flag);
        return "redirect:purchaseformlist.action";

    }
    //去添加采购订单控制器
    @RequestMapping("/toaddpurchaseform")
    public String toaddpurchaseform(int companyId, Model model, HttpSession session){
        logger.info("进入添加采购订单控制器");
        //将当前时间转换为字符串类型
        Date date=new Date();
        logger.info(companyId);
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddhhmmsssss");
        String str=format.format(date);
        //设置采购订单编号自动生成
        PurchaseForm purchaseForm=new PurchaseForm();
        purchaseForm.setPurchaseFormId(companyId+str);
        System.out.println(purchaseForm.getPurchaseFormId());
        //设置流程记录信息
        Object obj=session.getAttribute("currentUser");
        User user=null;
        if(obj!=null){
            user=(User)obj;
        }
        purchaseForm.setPurchaseRecord(user.getUserName()+"添加了一条采购订单信息");
        //根据公司编号查询公司人员信息
        Company company=companyService.findByCompanyId(companyId);
        System.out.println(company.getCompanyName());
        List<FormStats> list=formStatsService.findAll();
        //根据公司编号查询物品库存信息
        List<MaterialsVO> list1=materialsService.findByCompanyId(companyId);
        //将所得信息放进作用于中
        model.addAttribute("list1",list1);
        model.addAttribute("list",list);
        model.addAttribute("purchaseForm",purchaseForm);
        model.addAttribute("company",company);
        return "addpurchaseform";
    }
    //添加采购订单控制器
    @RequestMapping("/addpurchaseform")
    public  String addpurchaseform(int companyId, PurchaseForm purchaseForm){
        logger.info("进入到添加采购订单控制器");
        boolean flag=purchaseFormService.add(purchaseForm);
        logger.info(flag?"成功":"失败");
        return "redirect:http://localhost:8080/management/purchaseform/purchaseformlist.action?companyId="+companyId;
    }
}
